package org.baicaix.chat.views;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import org.baicaix.chat.ChatConstants;
import org.baicaix.chat.ChatMainFacade;



/**
 * @author Rocky Jiang
 * blog:www.blogjava.net/kissjava 
 * */
public class LoginDialog extends JDialog {

	private JLabel nameLabel = null;
	
	private JLabel passwordLabel  = null;
	
	private JTextField nameField = null;
	
	private JPasswordField passwordField = null;
	
	private JButton loginBtn = null;
	
	private JButton cancelBtn = null;
	
	private ChatMainFacade  facade = null;
	
	public LoginDialog(JFrame parent){
		super(parent, true);
		
		facade = ChatMainFacade.getInstance();
		
		jbInit();
		
		this.setAlwaysOnTop(true);
		setSize(350, 250);
		this.setResizable(false);
		this.toFront();
	}
	
	int labelWidth = 70;
	int hGap = 10;
	int fieldWidth = 220;
	private JLabel infoLabel = null;
	private void jbInit(){

		nameLabel = new JLabel("用户名：");
		passwordLabel = new JLabel("密  码：");
		nameField = new JTextField();
		passwordField = new JPasswordField();

		loginBtn = new JButton("登入");
		loginBtn.setActionCommand(LOGIN);
		loginBtn.addActionListener(new LoginDialogActionLisenter());
		cancelBtn = new JButton("取消");
		cancelBtn.setActionCommand(CANCLE);
		cancelBtn.addActionListener(new LoginDialogActionLisenter());
		int startX = 20;
		int startY = 20;

		infoLabel = new JLabel("用户名和密码输任意值(不能为空)");
		nameLabel.setBounds(startX, startY, labelWidth, 30);
		startX += labelWidth + hGap;
		nameField.setBounds(startX, startY, fieldWidth, 30);
		startX = 20;
		startY += 50;
		passwordLabel.setBounds(startX, startY, labelWidth, 30);
		startX += labelWidth + hGap;
		passwordField.setBounds(startX, startY, fieldWidth, 30);
		
		startX = 60;
		startY += 50;
		
		loginBtn.setBounds(startX, startY, 70, 30);
		
		startX += 110;
		cancelBtn.setBounds(startX, startY, 70, 30);
		
		startX = 60;
		startY += 50;
		infoLabel.setBounds(startX, startY, 250, 30);
		
		this.setLayout(null);
		this.add(nameLabel);
		this.add(nameField);
		this.add(passwordLabel);
		this.add(passwordField);
		
		this.add(loginBtn);
		this.add(cancelBtn);
		this.add(infoLabel);
	}
	
	public static final String LOGIN	=	"LOGING";
	public static final String CANCLE	=	"CANCEL";
	
	
	private void doLogin(){
		String name = this.nameField.getText();
		String pwd = this.passwordField.getText();
		if(name!=null && !"".equals(name) && pwd!=null && !"".equals(pwd)){
			Map data = new HashMap();
			data.put("name", name);
			facade.sendNotification(ChatConstants.LOGIN_SUCESS, data, null);
			this.dispose();
		}
		
	}
	
	private void doClose(){
		System.exit(1);
	}
                
	class LoginDialogActionLisenter implements ActionListener{
		 public void actionPerformed(ActionEvent e){
			 String command = e.getActionCommand();
			 if(LOGIN.equals(command)){
				 doLogin();
			 }else if(CANCLE.equals(command)){
				 doClose();
			 }
		 }
	}
}
